package com.iot.ic.service;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iot.ic.common.CustomException;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.dao.RefereeInfoMapper;
import com.iot.ic.model.RefereeInfo;
import com.iot.ic.model.UserLogin;
import com.iot.ic.utils.MD5Encryption;
import com.iot.ic.common.Options;




/**
 * Created by xiuge on 2017/2/20.
 */
@Service
public class RefereeServiceImpl implements RefereeService {

	@Autowired
	private RefereeInfoMapper refereeInfoMapper;
	

	@Autowired
	private LogService logService;
	
	
	@Autowired
	private UserLoginService userLoginService;
	

	
	@Override
	public RefereeInfo getRefereeInfoByRefereeNo(String refereeNo) {
		RefereeInfo refereeInfo = refereeInfoMapper.getByRefereeNo(refereeNo);
		return refereeInfo;
	}

	@Override
	public int addRefereeInfo(RefereeInfo refereeInfo) {
		int resultFlag = 0;
		if (refereeInfo.getRefereeNo() == null) {
			if (refereeInfo.getRefereeRole() == Options.CommonReferee.getCode()) {
				throw new CustomException(ReturnCode.Err_RefereeNoNull);
			} else if (refereeInfo.getRefereeRole() == Options.CommonUser.getCode()) {
				String phoneNum = refereeInfo.getRefereePhone();
				String userNo = phoneNum.substring(phoneNum.length()-4, phoneNum.length());
				refereeInfo.setRefereeNo(userNo);
			} else if (refereeInfo.getRefereeRole() == Options.HeadReferee.getCode()) {
				refereeInfo.setRefereeNo("0000");
			}
		}
		RefereeInfo oldRefereeInfo = refereeInfoMapper.getByRefereeNo(refereeInfo.getRefereeNo());
		if (oldRefereeInfo == null) {
			refereeInfo.setCreateTime(new Date());
			refereeInfo.setDelFlag(0);
			refereeInfo.setRefereePwd(MD5Encryption.getMD5("123456"));
			resultFlag = refereeInfoMapper.insert(refereeInfo);
		} else {
			throw new CustomException(ReturnCode.Err_RefereeNoExist);
		}
		return resultFlag;
	}

	@Override
	public int updateRefereeInfo(RefereeInfo RefereeInfo) {
		int flag = refereeInfoMapper.updateByPrimaryKeySelective(RefereeInfo);
		return flag;
	}

	@Override
	public List<RefereeInfo> listRefereeInfos(RefereeInfo RefereeInfo) {
		List<RefereeInfo> result = refereeInfoMapper.listRefereeInfos(RefereeInfo);
		return result;
	}

	@Override
	public int deleteRefereeInfo(int refereeId) {
		int result = refereeInfoMapper.deleteRefereeInfo(refereeId);
		return result;
	}

	@Override
	public UserLogin login(RefereeInfo RefereeInfo) {
		String refereeNo = RefereeInfo.getRefereeNo();
		String refereePwd = RefereeInfo.getRefereePwd();
		UserLogin userLogin = new UserLogin();
		if ( refereeNo == null || refereeNo.equals("") ) {
			throw new CustomException(ReturnCode.Err_LoginRefereeNoIsNull);
		}	
		if (refereeNo != null & refereePwd != null) {
			RefereeInfo oldUser = refereeInfoMapper.getByRefereeNo(refereeNo);			
			if (oldUser == null) {
				throw new CustomException(ReturnCode.Err_UserNotExist);
			} else {
				if (oldUser.getRefereePwd().equalsIgnoreCase(refereePwd)) {
					long currentTime = System.currentTimeMillis();
					Date loginTime = new Date(currentTime);
					String token = MD5Encryption.getMD5(String.valueOf(getRandomCount())+refereeNo);
					userLogin.setId(oldUser.getRefereeId());
					userLogin.setAccessToken(token);
					userLogin.setLastActiveTime(loginTime);
					userLogin.setUserName(oldUser.getRefereeName());
					userLogin.setUserType(oldUser.getRefereeRole());
					logService.addLogs(oldUser.getRefereeNo(), oldUser.getRefereeName(), new Date(), "登录成功", "");
					userLoginService.addUserLogin(userLogin);
				} else {
					throw new CustomException(ReturnCode.Err_PwdError);
				}
			}		
		}
		return userLogin;
	}
	
	/**
	 * 生成一个随机数
	 * @return
     */
	private int getRandomCount(){
		double num=Math.random();
		return  (int)(num*10);
	}

	@Override
	public int changePwd(RefereeInfo refereeInfo) {
		int result = 0;
		RefereeInfo oldrefereeInfo = refereeInfoMapper.selectByPrimaryKey(refereeInfo.getRefereeId());
		if (!oldrefereeInfo.getRefereePwd().equalsIgnoreCase(refereeInfo.getOldRefereePwd())) {
		    throw new CustomException(ReturnCode.Err_OldPassIsWrong);
		} else {
			result = refereeInfoMapper.updateByPrimaryKeySelective(refereeInfo);
		}
		
		return result;
	}

	@Override
	public RefereeInfo getRefereeInfo(int refereeInfoId) {
		RefereeInfo refereeInfo = refereeInfoMapper.selectByPrimaryKey(refereeInfoId);
		return refereeInfo;
	}

	@Override
	public List<RefereeInfo> listUsers(RefereeInfo RefereeInfo) {
		List<RefereeInfo> userList = refereeInfoMapper.listUsers(RefereeInfo);
		return userList;
	}
	
}
